//获取登录用户名
var rename = tool.getCookie('rename')
var user_name = tool.getCookie("username")
if(rename || user_name){
    var res = rename || user_name
    $(".nav .a_login").css("display","none").next().css("display","none")
    $(".nav .a_register").css("display","none").next().css("display","none")
    $(".user_box").css("display","block")
    $(".user_id").text(res)
}
$(".exit").click(function(){
    tool.setCookie('rename',$('#usname').val(),-1000)
    tool.setCookie('username',$('#usname').val(),-1000)
    location.href="./detail.html"
})
$(".nav .a_login").click(function(){
    localStorage.setItem("url",location.href)
    location.href="./login.html"
})
$(".a_register").click(function(){
    location.href="./register.html"
})
$(".img").click(function(){
    location.href="./index.html"
})


//
var arr = location.search.match(/id=(\d+)/)
// if(!arr){
//     layer.msg("非法访问",{
//         icon:2,
//         time:2000
//     },function(){
//         location.href = "list.html";
//     })
// }
var id = arr[1];
$.ajax({
    url:"./php/history.php",
    data:{
        username:res,
        goodsid:id
    },
    method:"post"
})
$.ajax({
    url:'php/hot.php',
    data:{id}
})
var loadindex = layer.load(1,{
    shade: [0.5,'#666']
})
console.log(1)
$.ajax({
    url:"../php/detail.php",
    data:{id},
    dataType:"json",
    success:res=>{
        layer.close(loadindex)
        var {data} = res;
        console.log(data)
        var smallImgs = data.manyImg.split("==========")
        var str = "";
        smallImgs.forEach(item=>{
            str+=`
            <img src ="${item}">`
        })
        console.log(str)
        $(".small").html(str);
        $(".small").attr("data-id",data.id);
        $(".middle>img").attr("src",smallImgs[0])
        $(".small>img").first().addClass("active")
        $(".big>img").attr("src",smallImgs[0])
        $(".s_title").children("b").html(data.name)
        $(".introduce").html(data.name)
        $(".now_price").html(data.price)
        tab()
        enlarge()
    }
})

//放大镜
function tab(){
    $(".small img").click(function(){
        $(this).addClass("active").siblings().removeClass("active").parent().prev().children().eq(1).attr("src",$(this).attr("src")).next().children().attr("src",$(this).attr("src"))
    });
}
function enlarge(){
    $(".middle").mousemove(function(e){
        var x=e.pageX-$(this).children().first().width()/2;
        var y=e.pageY-$(this).children().first().height()/2;
        // console.log($(this))
        if(x<$(this).offset().left){
            x=$(this).offset().left
        }else if(x>$(this).offset().left+$(this).width()-$(".shade").width()){
            x=$(this).offset().left+$(this).width()-$(".shade").width()
        }
        if(y<$(this).offset().top){
            y=$(this).offset().top
        }else if(y>$(this).offset().top+$(this).height()-$(".shade").height()){
            y=$(this).offset().top+$(this).height()-$(".shade").height()
        }
        $(this).children().first().css("display","block").offset({
            left:x,
            top:y
        }).next().next().css("display","block").children().css({
            left:-$('.shade').position().left/$(this).width()*$(".big>img").width(),
            top:-$('.shade').position().top/$(this).height()*$(".big>img").height()
        })
    })
    $(".middle").mouseleave(function(){
        $(".shade").css("display","none").next().next().css("display","none")
    })
    
}
// if(!res){
    
//         localStorage.setItem("url",location.href)
//         // location.href = "./login.html"
  
// }
//加入购物车
var res=rename || user_name
console.log(res)
$(".pay_for").click(function(){
    if(!res){
        layer.msg("请先登录您的账户",{
            icon:2,
            time:1500
        },function(){
            localStorage.setItem("url",location.href)
            location.href = "./login.html"
        })
        return false;
    }
    var str = localStorage.getItem("cartData");
    if(str){
        var arr = JSON.parse(str);
        console.log(str);
        var data = arr.find(item=>item.username === res && item.id ===id)
        if(data){
            data.number++
            localStorage.setItem("cartData",JSON.stringify(arr))
            var number=$(".dec").prev().prev().val()-0;
            number++;
            $(".dec").prev().prev().val(number);
        }else{
            var obj = {
                id,
                number:1,
                username:res
            }
            arr.push(obj);
            console.log(arr)
            localStorage.setItem("cartData",JSON.stringify(arr))
        }
    }else{
        var obj={
            id,
            number:1,
            username:res
        }
        var arr = [];
        arr.push(obj)
        localStorage.setItem("cartData",JSON.stringify(arr))
    }
    layer.msg("购物车添加成功,请移步购物车页面结算",{
        icon:1,
        time:1500
    })
    // var number=$(".dec").prev().prev().val()-0;
    // number++;
    // $(".dec").prev().prev().val(number);
    // var data = JSON.parse(localStorage.getItem("cartData"));
    // var id = $(".small").attr("data-id");
    // var currentData = data.find(item=>item.username === res && item.id == id);
    // console.log(id)
    // currentData.number = number;
    // localStorage.setItem("cartData",JSON.stringify(data))
    return false
})
$(".cart").click(function(){
    if(res){
        location.href="./shopping.html"
    }else{
        layer.msg("请先登录您的账户",{
            icon:2,
            time:1500
        })
    }
})

$(".dec").click(function(){
    var number=$(this).prev().prev().val()-0;
    number--;
    if(number<=1){
        number=1;
    }
    $(this).prev().prev().val(number);
    var data = JSON.parse(localStorage.getItem("cartData"));
    console.log(data)
    var id = $(".small").attr("data-id");
    // var currentData = data.find(item=>item.username === res && item.id == id);
    // currentData.number = number;
    localStorage.setItem("cartData",JSON.stringify(data))
    return false
}) 
$(".add").click(function(){
 var number=$(this).prev().val()-0;
 number++;
 console.log($(this).prev().val())
//  if(number>=$(this).prev().prev().attr("max")){
//      number=$(this).prev().prev().attr("max");
//  }
 $(this).prev().val(number);
 var data = JSON.parse(localStorage.getItem("cartData"));
 var id = $(".small").attr("data-id");
 var currentData = data.find(item=>item.username === res && item.id == id);
 console.log(id)
 currentData.number = number;
 localStorage.setItem("cartData",JSON.stringify(data))
 return false;
}) 